﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECCIT.Data;
using System.Data;
using System.Data.SqlClient;

using ECCIT.Business.Model;
using OgilvyOne.UKNow.Model;

namespace OgilvyOne.UKNow.DAL
{
    public class CnEnMappingDAL
    {
        public CnEnMappingMod GetCnEnMod(int langID, int eventID,int typeID)
        {
            string cmd = "";
            #region
            if (langID == 2052)
            {
                cmd = string.Format("select * from uknow_lang_mapping where col_data_type={0} and col_data_id_cn={1}",typeID, eventID.ToString());
            }

            else
            {
                cmd = String.Format("select * from uknow_lang_mapping where col_data_type={0}  and col_data_id_en={1}",typeID, eventID.ToString());
            }
            #endregion
            #region
            if (cmd != "")
            {
                DataSet ds = DBHelper.ExecuteDataSet(cmd, null);
                if (ds != null)
                {
                    CnEnMappingMod cem = new CnEnMappingMod();
                    DataTable dt = ds.Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        cem.Col_data_id_cn = Convert.ToInt32(dr["COL_DATA_ID_CN"]);
                        cem.Col_data_id_en = Convert.ToInt32(dr["COL_DATA_ID_EN"]);
                    }
                    return cem;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
            #endregion
        }

        //根据中文活动ID获取数据记录
        public CnEnMappingMod GetCnEnAppModel(int typeID, int Cid,int? Eid,int? mainID)
        {
            StringBuilder cmd = new StringBuilder();
            cmd.AppendFormat("select * from uknow_lang_mapping where col_data_type={0}  and (col_data_id_cn={1} or col_data_id_en={2}) ", typeID, Cid,Eid);
            if (mainID != null)
            { cmd.AppendFormat("and  col_mapping_id!={0}", mainID); }

            DataSet ds = DBHelper.ExecuteDataSet(cmd.ToString(), null);
            //CnEnMappingMod model = new CnEnMappingMod();
            if (ds.Tables[0].Rows.Count>0)
            {
                CnEnMappingMod cem = new CnEnMappingMod();
                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cem.Col_data_id_cn = Convert.ToInt32(dr["COL_DATA_ID_CN"]);
                    cem.Col_data_type = Convert.ToInt32(dr["col_data_type"]);
                    cem.Col_data_id_en = Convert.ToInt32(dr["COL_DATA_ID_EN"]);
                    cem.Col_mapping_id = Convert.ToInt32(dr["COL_MAPPING_ID"]);
                }
                return cem;
            }
            else
            {
                return null;
            }
        }
        //获取全部的映射记录
        public DataTable getCnEnMappData(int typeID)
        {
            string sqlcmd = @"select a.COL_MAPPING_ID, a.COL_DATA_ID_CN,b.COL_EVENTS_TITLE,a.COL_DATA_ID_EN,c.COL_EVENTS_TITLE as COL_EVENTS_TITLE_EN from UKNOW_LANG_MAPPING  as a
                             left join UKNOW_EVENTS  as b on a.COL_DATA_ID_CN=b.COL_EVENTS_ID and b.COL_LANG_ID=2052 left join UKNOW_EVENTS 
                             as c on a.COL_DATA_ID_EN=c.COL_EVENTS_ID and c.COL_LANG_ID=1033
                             where a.COL_DATA_TYPE=" + typeID;

            DataSet ds = DBHelper.ExecuteDataSet(sqlcmd, null);

            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
                return dt;
            }
            else
            {
                return null;
            }
        }
        //根据中文活动ID删除映射数据记录
        public bool delete(int id, int typeID)
        {
            string sqlcmd =String.Format("delete from  uknow_lang_mapping where col_data_type={0} and col_data_id_cn={1}",typeID,id);
            int i = DBHelper.ExecuteNonQuery(sqlcmd);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //添加一条映射记录
        public bool Add(CnEnMappingMod model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into uknow_lang_mapping(col_data_type,col_data_id_cn,col_data_id_en) values (@col_data_type,@col_data_id_cn,@col_data_id_en)");
            IDataParameter[] parameters = {
					new SqlParameter("@col_data_type", SqlDbType.Int,4),
					new SqlParameter("@col_data_id_cn", SqlDbType.NVarChar,50),
					new SqlParameter("@col_data_id_en", SqlDbType.NVarChar,50)
                                          };

            parameters[0].Value = model.Col_data_type;
            parameters[1].Value = model.Col_data_id_cn;
            parameters[2].Value = model.Col_data_id_en;

            return DBHelper.ExecuteNonQuery(strSql.ToString(), parameters) > 0;
        }
        //修改一条映射数据记录
        public bool Update(CnEnMappingMod model)
       {
           StringBuilder strSql = new StringBuilder();

           strSql.Append("update uknow_lang_mapping set col_data_type=@col_data_type,col_data_id_cn=@col_data_id_cn,col_data_id_en=@col_data_id_en where col_mapping_id=@col_mapping_id");
           IDataParameter[] parameters = {
                    new SqlParameter("@col_data_type", SqlDbType.Int),
                    new SqlParameter("@col_data_id_en", SqlDbType.Int),
                    new SqlParameter("@col_data_id_cn", SqlDbType.Int),
                    new SqlParameter("@col_mapping_id", SqlDbType.Int)
                                         };
					
           parameters[0].Value = model.Col_data_type;
           parameters[1].Value = model.Col_data_id_en;
           parameters[2].Value = model.Col_data_id_cn;
           parameters[3].Value = model.Col_mapping_id;
           
           return DBHelper.ExecuteNonQuery(strSql.ToString(), parameters) > 0;   
           
        }
  
        
    }     
}
    


    